package com.base.vistter.iframe.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.vistter.iframe.bean.SystemContextHolder;
import com.base.vistter.iframe.constant.CommonConstant;
import com.base.vistter.iframe.entity.SysMenu;
import com.base.vistter.iframe.entity.SysRoleMenu;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.SysMenuMapper;
import com.base.vistter.iframe.utils.ToolUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class SysMenuService extends BaseService<SysMenuMapper, SysMenu> {

    @Resource(name = "sysRoleMenuService")
    private SysRoleMenuService sysRoleMenuService;

    @Transactional(readOnly = true)
    public List<SysMenu> getList(String name) throws IframeException {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<SysMenu>();
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like(SysMenu::getName, name);
        }
        return this.selectList(queryWrapper);
    }

    @Transactional(readOnly = true)
    public List<SysMenu> getTreeData(Integer code) throws IframeException {
        if (code == null) {
            return super.selectList();
        }
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<SysMenu>();
        if (code == 1 || code == 2) {
            queryWrapper.eq(SysMenu::getType, 1);
        } else if (code == 3) {
            queryWrapper.in(SysMenu::getType, 1, 2);
        }
        return this.selectList(queryWrapper);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean save(SysMenu entity) throws IframeException {
        boolean result = super.save(entity);
        if (entity.getParentId() == null) {
            entity.setParentPath(String.valueOf(entity.getId()));
        } else {
            SysMenu parent = super.getById(entity.getParentId());
            entity.setParentPath(parent.getParentPath() + PARENT_PATH_DIVIDE + entity.getId());
        }
        super.updateById(entity);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateById(SysMenu entity) throws IframeException {
        SysMenu persistence = super.getById(entity.getId());
        if (entity.getParentId() == null) {
            entity.setParentPath(String.valueOf(entity.getId()));
            //如果如果父节点路径发生变化
            if (!Objects.equals(persistence.getParentId(), entity.getParentId())) {
                this.updateChildParentPath(persistence.getParentPath(), entity);
            }
        } else {
            SysMenu parent = super.getById(entity.getParentId());
            entity.setParentPath(parent.getParentPath() + PARENT_PATH_DIVIDE + entity.getId());
            //如果如果父节点路径发生变化
            if (!Objects.equals(persistence.getParentId(), entity.getParentId())) {
                this.updateChildParentPath(persistence.getParentPath(), entity);
            }
        }
        return super.updateById(entity);
    }

    private void updateChildParentPath(String parentPath, SysMenu sysMenu) throws IframeException {
        this.baseMapper.updateParentPath(parentPath, sysMenu.getParentPath());
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysMenu> getUserMenu() throws IframeException {
        if (ToolUtils.isAdmin()) {
            LambdaQueryWrapper<SysMenu> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(SysMenu::getStatus, CommonConstant.STATUS_NORMAL);
            return super.selectList(lambdaQueryWrapper);
        } else {
            List<SysRoleMenu> list = sysRoleMenuService.getRoleMenuList(SystemContextHolder.getSessionContext().getRoleId());
            if (list != null && !list.isEmpty()) {
                List<Integer> menuIds = list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
                return this.getSysMenuByIds(menuIds, 1, 2, 3);
            } else {
                return new ArrayList<SysMenu>();
            }
        }
    }

    @Transactional(readOnly = true)
    public List<SysMenu> getEnableMenu() throws IframeException {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<SysMenu>();
        queryWrapper.eq(SysMenu::getStatus, 0);
        if (!ToolUtils.isAdmin()) {
            List<Integer> menuIds = sysRoleMenuService.getMenuIds(SystemContextHolder.getSessionContext().getRoleId());
            if (menuIds == null || menuIds.isEmpty()) {
                return null;
            } else {
                queryWrapper.in(SysMenu::getId, menuIds);
            }
        }

        return super.selectList(queryWrapper);
    }

    @Transactional(readOnly = true)
    public List<SysMenu> getSysMenuByIds(List<Integer> menuIds, Integer... type) throws IframeException {
        if (menuIds == null || menuIds.isEmpty()) {
            return new ArrayList<SysMenu>();
        }
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<SysMenu>();
        queryWrapper.in(SysMenu::getId, menuIds).in(SysMenu::getType, Arrays.asList(type)).eq(SysMenu::getStatus, CommonConstant.STATUS_NORMAL);
        return super.selectList(queryWrapper);
    }

}
